var $,form,upload,element,layer;
var user = JSON.parse(localStorage.getItem('user'));
var userId;
layui.use(['jquery','form','upload', 'element', 'layer'], function() {
    $ = layui.jquery;
    form = layui.form;
    upload = layui.upload;
    element = layui.element;
    layer = layui.layer;
    $("input[name=account]").val(user.account);
    $("input[name=userId]").val(user.userId);
    $("input[name=email]").val(user.email);
    $("#avatarUrl").attr("src",user.acatarUrl);
    userId = $("input[name=userId]").val();
    //上传图片
    var uploadInst = upload.render({
        elem: '#upload_pictures',
        url: '/Upload/upload',
        accept: 'file',
        field:'file',
        auto:true,
        accept:'images',
        acceptMime: 'image/jpeg,image/png',
        ext: 'jpg|png|bmp|jpeg',
        size: 4096,
        done: function(res){
            user.acatarUrl = res.data;
            localStorage.setItem("user",JSON.stringify(user))
            $('#avatarUrl').attr('src', res.data);
            parent.$('#headDiagram').attr('src',res.data);
            $('input[name = acatarUrl]').val(res.data);
            $.ajax({
                url:'/systemIndex/updateUser',
                type: 'post',
                dataType:'json',
                data:{
                    "userId": userId,
                    "acatarUrl": res.data
                },
                success:function (data) {
                    if(data.data){
                        layer.msg("修改成功");
                    }else{
                        layer.msg("修改失败");
                    }
                }
            });
        }
    });
})

//提交
$("#sub").on('click',function(){
    var conpwd=$("input[name=confirmpassword]").val();
    var newpwd=$("input[name=newpassword]").val();
    if(newpwd != '' && conpwd != '' && conpwd == newpwd){
        $.ajax({
            url : '/systemIndex/updateUser',
            type : "post",
            dataType : "json",
            data : {
                "userId": userId,
                "password": conpwd,
            },
            success : function(data) {
                if(data.data){
                    document.getElementById("x_password").value="";
                    document.getElementById("qr_password").value="";
                    document.getElementById("x_password").setAttribute("placeholder","请输入您的新密码");
                    document.getElementById("qr_password").setAttribute("placeholder","请确认您输入的密码");
                    document.getElementById("pwdmsg").innerHTML="*";
                    document.getElementById("pwd1msg").innerHTML="*";
                    layer.msg("修改成功");
                }else{
                    layer.msg("修改失败");
                }
            }
        })
        return false;
    }else if(newpwd == '' || conpwd == ''){
        layer.msg("请填充数据！！！");
        return false;
    }else if (conpwd != newpwd){
        layer.msg("密码输入不一致！！！");
        return false;
    }
})
//密码校验
document.formDate.newpassword.onblur=function(){
    var reg1=/[A-Za-z]+[0-9]+/;
    var conpwd=$("input[name=confirmpassword]").val();
    var newpwd=$("input[name=newpassword]").val();
    if(reg1.test(this.value)&&this.value.length>=6&&this.value.length<=8){
        console.log("符合标准");
        document.getElementById("pwdmsg").innerHTML="<img src='/images/icon20.png' width='15' height='15'>";
        if (conpwd != '' && newpwd != conpwd){
            document.getElementById("pwd1msg").innerHTML="<img src='/images/icon19.gif' width='15' height='15'><span>两次输入密码不一致</span>";
        }else if (conpwd != '' && newpwd == conpwd){
            document.getElementById("pwd1msg").innerHTML="<img src='/images/icon20.png' width='15' height='15'>";
        }
    }else{
        if (newpwd != ''){
            if ((newpwd.length > 0 && newpwd.length < 6) || newpwd.length > 8){
                document.getElementById("pwdmsg").innerHTML="<img src='/images/icon19.gif' width='15' height='15'><span>请确认密码长度在6-8个字符</span>";
            }
        }else{
            document.getElementById("x_password").value="";
            document.getElementById("x_password").setAttribute("placeholder","请输入您的新密码");
        }
    }
}
//确认密码
document.formDate.confirmpassword.onblur=function(){
    var conpwd=$("input[name=confirmpassword]").val();
    var newpwd=$("input[name=newpassword]").val();
    if(newpwd == '' && conpwd != ''){
        if ((conpwd.length > 0 && conpwd.length < 6) || conpwd.length > 8){
            document.getElementById("pwd1msg").innerHTML="<img src='/images/icon19.gif' width='15' height='15'><span>请确认密码长度在6-8个字符</span>";
        }else{
            document.getElementById("pwd1msg").innerHTML="<img src='/images/icon19.gif' width='15' height='15'><span>两次输入密码不一致</span>";
        }
    }else if(newpwd != ''&&conpwd != ''&&newpwd != conpwd){
        document.getElementById("pwd1msg").innerHTML="<img src='/images/icon19.gif' width='15' height='15'><span>两次输入密码不一致</span>";
        if ((conpwd.length > 0 && conpwd.length < 6) || conpwd.length > 8){
            document.getElementById("pwd1msg").innerHTML="<img src='/images/icon19.gif' width='15' height='15'><span>请确认密码长度在6-8个字符</span>";
        }
    }else if(newpwd != ''&&conpwd == ''){
        document.getElementById("pwd1msg").innerHTML="<img src='/images/icon19.gif' width='15' height='15'><span>确认密码不能为空</span>";
    }else if(newpwd != ''&&newpwd == conpwd){
        if ((conpwd.length > 0 && conpwd.length < 6) || conpwd.length > 8){
            document.getElementById("pwd1msg").innerHTML="<img src='/images/icon19.gif' width='15' height='15'><span>请确认密码长度在6-8个字符</span>";
        }else{
            document.getElementById("pwd1msg").innerHTML="<img src='/images/icon20.png' width='15' height='15'>";
        }
    }else if (newpwd == '' && conpwd == ''){
        document.getElementById("qr_password").value="";
        document.getElementById("qr_password").setAttribute("placeholder","请确认您输入的密码");
    }
}
//刷新
$("#refresh").on("click",function () {
    location.reload();
})

//返回首页
function backOff(){
    parent.jumpSystemHome();
}
